/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;
using System.IO;
using System.Data;
using System.Text;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// File arithmetic for outputing table
	/// </summary>
	[Serializable]
	public class FileArithmetic: EQObject,  IBatchMode 
	{
		private string mSeparator = "`";
		/// <summary>
		/// Set/Get the splitor of each column
		/// </summary>
		public char Separator
		{
			get
			{
				return this.mSeparator[0];
			}
			set
			{
				this.mSeparator = value.ToString();
			}
		}

		/// <summary>
		/// Implement the interface
		/// </summary>
		/// <param name="objRange"></param>
		/// <param name="objEQTableBase"></param>
		public void ArithmeticOutput(Excel.Range objRange, EQTableBase objEQTableBase)
		{
			string strFileName = this.OutputTextFile(objEQTableBase);
			string strConnection = "TEXT;" + strFileName;
			Excel.Worksheet objWorksheet = objRange.Worksheet;
			Excel.QueryTable objQueryTable;
			objQueryTable = objWorksheet.QueryTables.Add(strConnection, objRange, this.Missing);

			objQueryTable.TextFilePlatform = Excel.XlPlatform.xlWindows;//-535;	//Encode, here using Unicode(UTF-8), its value is -535
			objQueryTable.TextFileStartRow = 2;	//First row in text file
			objQueryTable.TextFileParseType = Excel.XlTextParsingType.xlDelimited;
			objQueryTable.TextFileTextQualifier = Excel.XlTextQualifier.xlTextQualifierNone;	//When input text file, whether the string message is limited by any qualifiers,ect ' and "
			objQueryTable.TextFileConsecutiveDelimiter = false;
			objQueryTable.TextFileTabDelimiter = false;	
			objQueryTable.TextFileSemicolonDelimiter = false;
			objQueryTable.TextFileCommaDelimiter = false;
			objQueryTable.TextFileSpaceDelimiter = false;
			objQueryTable.TextFileOtherDelimiter = this.mSeparator;	//Splitor for text separating
			objQueryTable.TextFileColumnDataTypes = GetDataType(objEQTableBase);	//Set tyle of each column 

			objQueryTable.Refresh(false);
			objQueryTable.Delete();
			File.Delete(strFileName);	//Delete temporary file after read all text to excel document
		}


		/// <summary>
		/// Output datatable to a text file with encoding Unicode-UTF8. Return file name.
		/// <returns></returns>
		/// </summary>
		/// <param name="objEQTableBase"></param>
		private string OutputTextFile(EQTableBase objEQTableBase)
		{
			StringBuilder objStringBuilder = new StringBuilder();
			objStringBuilder.Append("\r\n");
			for(int i=0; i<objEQTableBase.Table.Rows.Count; i++)
			{
				for(int j=0; j<objEQTableBase.Table.Columns.Count; j++)
					objStringBuilder.Append(objEQTableBase.Table.Rows[i][j].ToString().Replace(this.mSeparator, "") + this.mSeparator);
				objStringBuilder.Append("\r\n");
			}

			string strFileName = EQWebKit.GenerateAutoFullFileName(EQConfiguration.TemporaryFilePath, ".txt");

			//Write string to text file
			StreamWriter objStreamWriter = null;
			try
			{
				objStreamWriter = new StreamWriter(strFileName, false, Encoding.UTF8);
				objStreamWriter.Write(objStringBuilder.ToString());
			}
			finally
			{
				if(objStreamWriter!=null)
					objStreamWriter.Close();
			}
			return strFileName;
		}


		/// <summary>
		/// Get collection of each column's type
		/// </summary>
		/// <returns></returns>
		/// <param name="objEQTableBase"></param>
		private static object[] GetDataType(EQTableBase objEQTableBase)
		{
			object[] array = new object[objEQTableBase.TableColumnsCount];
			for(int i=0; i<objEQTableBase.TableColumnsCount; i++)
			{
				if(objEQTableBase.ColumnsDataType[i] == EQType.General)
					array[i] = Excel.xlColumnDataType.xlGeneralFormat;
				else
					array[i] = Excel.xlColumnDataType.xlTextFormat;
			}
			return array;
		}
	}
}
